package com.ruoyi.ftrl.service;

import com.ruoyi.common.config.MessageEventListeners;
import com.ruoyi.common.config.MessageEventListenerss;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.ftrl.common.CodeUtil;
import com.ruoyi.ftrl.domain.EquipmentHeat;
import com.ruoyi.ftrl.domain.HeatInfo;
import com.ruoyi.ftrl.mapper.EquipmentHeatMapper;
import com.ruoyi.ftrl.mapper.HeatInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RedisMessageHandlerss implements MessageEventListenerss {
    @Autowired
    //Redis工具类
    RedisCache redisCache;
    @Autowired
     IHeatInfoService heatInfoService;
    @Autowired
    HeatInfoMapper heatInfoMapper;
    @Autowired
    EquipmentHeatMapper equipmentHeatMapper;

    /**
     * 监听热表数据
     * @param message
     */
    @Override
    public void onMessage(String message) {
        System.err.println("收到的热表信息为----------"+message);
        HeatInfo heatInfo = CodeUtil.HexToNew(message);

        //将监听到的热表数据进行持久
        System.err.println(heatInfo);

        //HeatInfo heatInfo=new HeatInfo();
        EquipmentHeat equipmentHeat = equipmentHeatMapper.selectEquipmentHeatByCode(heatInfo.getCode());
        //小区
        heatInfo.setVillageName(equipmentHeat.getCommunityName());
        //楼栋
        heatInfo.setBuildingName(equipmentHeat.getBuildingName());
        //单元
        heatInfo.setUnitName(equipmentHeat.getUnitName());
        //房间号
        heatInfo.setRoomName(equipmentHeat.getBuildingName()+"-"+equipmentHeat.getUnitName()+"-"+equipmentHeat.getRoomAme());
        heatInfo.setDeviceCode(heatInfo.getCode());
        heatInfo.setRoomNo(equipmentHeat.getRoomNo());
        heatInfo.setManufactor("1");
        heatInfo.setCreateTime(new Date());
        heatInfo.setHistory("1");
        heatInfo.setState("0");


        HeatInfo heatInfoByCode = heatInfoMapper.selectHeatInfoByCode(heatInfo.getCode());
        System.err.println(heatInfo.getCode());
        if (heatInfoByCode != null) {
            //修改为历史记录
            System.err.println("不为空！！！");
            heatInfoMapper.updateHeatInfoId(heatInfoByCode.getId());
            heatInfoMapper.insertHeatInfo(heatInfo);
        }else {
            //持久化
       heatInfoMapper.insertHeatInfo(heatInfo);
        }



    }

}
